package com.tmptSource.service.impl;

import com.tmpt.dto.Result;
import com.tmpt.utils.DateTool;
import com.tmpt.service.impl.DataLimitService;
import com.tmptSource.bean.OwrRegion;
import com.tmptSource.mapper.OwrEntityListInfoMapper;
import com.tmptSource.bean.OwrEntityListInfo;
import com.tmptSource.dto.OwrEntityListInfoDto;
import com.tmptSource.dto.PageDto;
import com.tmptSource.mapper.OwrRegionMapper;
import com.tmptSource.service.intf.IOwrEntityListInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tmptSource.utils.dozer.MyMapper;
import com.tmptSource.utils.page.MyPageUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import org.dozer.Mapper;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.apache.log4j.Logger;

/**
 * Created by wxy on 2016/7/28.
 */
@Service(value = "owrEntityListInfoService")
@Transactional(propagation = Propagation.REQUIRED)
public class OwrEntityListInfoService implements IOwrEntityListInfoService {

    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private DataLimitService dataLimitService;
    @Autowired
    private Mapper mapper;
    @Autowired
    private MyMapper myMapper;
    @Autowired
    private MyPageUtils myPageUtils;
    @Autowired
    private OwrEntityListInfoMapper owrEntityListInfoMapper;
    @Autowired
    private OwrRegionMapper owrRegionMapper;


    @Override
    public Result<OwrEntityListInfoDto> pagination(OwrEntityListInfoDto owrEntityListInfoDto, PageDto pageDto) {
        //分页查询
        Result<OwrEntityListInfoDto> result = myPageUtils.pagination(
                owrEntityListInfoDto, pageDto,
                OwrEntityListInfo.class,
                OwrEntityListInfoDto.class,
                owrEntityListInfoMapper,
                false, null);

        return result;
    }

    /**
     * 分布查询，有数据边界控制功能
     *
     * @param owrEntityListInfoDto
     * @param pageDto
     * @param paramMap
     * @return
     */
    @Override
    public Result<OwrEntityListInfoDto> pagination(OwrEntityListInfoDto owrEntityListInfoDto, PageDto pageDto, Map<String, Object> paramMap) {
        //数据边界控制参数
        Map<String, Object> paramMap1 = dataLimitService.getLimitParam(paramMap);

        //分页查询
        Result<OwrEntityListInfoDto> result = myPageUtils.pagination(
                owrEntityListInfoDto, pageDto,
                OwrEntityListInfo.class,
                OwrEntityListInfoDto.class,
                owrEntityListInfoMapper,
                paramMap1,
                false, null);

        return result;
    }


    @Override
    public Result<OwrEntityListInfoDto> selectAll() {
        Result<OwrEntityListInfoDto> result = new Result();

        //排序
        PageDto pageDto = new PageDto();
        pageDto.setSortColumn("id");
        pageDto.setSortOrder("desc");
        Map<String, Object> mapParam = new HashMap<>();
        mapParam.put("page", pageDto);

        List<OwrEntityListInfo> list = owrEntityListInfoMapper.selectAll(mapParam);
        List<OwrEntityListInfoDto> dtoList = myMapper.map(list, OwrEntityListInfoDto.class);
        result.setResultList(dtoList);

        return result.success();
    }

    @Override
    public Result<OwrEntityListInfoDto> selectById(long id) {
        Result<OwrEntityListInfoDto> result = new Result();

        OwrEntityListInfo owrEntityListInfo = owrEntityListInfoMapper.selectById(id);
        OwrEntityListInfoDto dto = mapper.map(owrEntityListInfo, OwrEntityListInfoDto.class);
        result.setResultObj(dto);

        return result.success();
    }


    /**
     * 返回ID值
     */
    @Override
    public Result<OwrEntityListInfoDto> addData(OwrEntityListInfoDto owrEntityListInfoDto) {
        Result<OwrEntityListInfoDto> result = new Result();

        //封闭数据
        OwrEntityListInfo owrEntityListInfo = mapper.map(owrEntityListInfoDto, OwrEntityListInfo.class);
        owrEntityListInfo.setCreatedate(DateTool.getCurTimestamp());

        //get type info by regionId
        Long regionId = owrEntityListInfo.getOwrRegionId();
        OwrRegion owrRegion = owrRegionMapper.selectById(regionId);
        Long owrTypeId = owrRegion.getOwrTypeId();
        try {
            owrEntityListInfo.setOwrType(Integer.parseInt(owrTypeId.toString()));
        }catch (Exception e){}

        //调用mapper
        long i = owrEntityListInfoMapper.insert(owrEntityListInfo);
        if (i == 0) {
            return result.fault("插入失败");
        }

        OwrEntityListInfoDto dto = mapper.map(owrEntityListInfo, OwrEntityListInfoDto.class);
        result.setResultObj(dto);

        return result.success();
    }

    /**
     * 返回ID值
     */
    @Override
    public Result<OwrEntityListInfoDto> editData(OwrEntityListInfoDto owrEntityListInfoDto) {
        Result<OwrEntityListInfoDto> result = new Result();

        //封装数据
        OwrEntityListInfo owrEntityListInfo = mapper.map(owrEntityListInfoDto, OwrEntityListInfo.class);
        owrEntityListInfo.setModifydate(DateTool.getCurTimestamp());

        //get type info by regionId
        Long regionId = owrEntityListInfo.getOwrRegionId();
        OwrRegion owrRegion = owrRegionMapper.selectById(regionId);
        Long owrTypeId = owrRegion.getOwrTypeId();
        try {
            owrEntityListInfo.setOwrType(Integer.parseInt(owrTypeId.toString()));
        }catch (Exception e){}


        //调用mapper
        long i = owrEntityListInfoMapper.updateById(owrEntityListInfo);
        if (i == 0) {
            return result.fault("编辑数据失败");
        }

        OwrEntityListInfoDto dto = mapper.map(owrEntityListInfo, OwrEntityListInfoDto.class);
        result.setResultObj(dto);

        return result.success();
    }

    /**
     * 伪删除记录，返回ID值
     */
    @Override
    public Result deleteById(long id, String adminMan) {
        Result result = new Result();

        Map<String, Object> mapParameter = new HashMap();
        mapParameter.put("id", id);
        mapParameter.put("modifyman", adminMan);
        mapParameter.put("modifydate", DateTool.getCurTimestamp());

        //调用mapper
        long i = owrEntityListInfoMapper.deleteById(mapParameter);
        if (i == 0) {
            return result.fault("删除数据失败");
        }

        return result.success();
    }


    /**
     * 页面上查询列的值，动态加载，sql语句读数据库配置而来
     */
    @Override
    public java.util.List select4owrRegionId() {
        return owrEntityListInfoMapper.select4owrRegionId();
    }

    @Override
    public java.util.List select4BuildingAndKitchenowrRegionId(long id) {
        return owrEntityListInfoMapper.select4BuildingAndKitchenowrRegionId(id);
    }

//    @Override
//    public Result<OwrEntityListInfoDto> selectById(long id) {
//        Result<OwrEntityListInfoDto> result = new Result();
//
//        OwrEntityListInfo owrEntityListInfo = owrEntityListInfoMapper.selectById(id);
//        OwrEntityListInfoDto dto = mapper.map(owrEntityListInfo, OwrEntityListInfoDto.class);
//        result.setResultObj(dto);
//
//        return result.success();
//    }

    @Override
    public Result<OwrEntityListInfoDto> getOwrEntityListByIds(String[] ids) {
        return null;
    }


}
